<?php
    @session_start();
    require_once 'shared-functions.php';
    require_once 'session.php';
    require_once 'masterpage.php';

    if(!IsValidSession())
    {
        header('Location: login.php?page=index');
        exit();
    }
    else
    {
        RefreshSession();
    }
	
	$user = GetCurrentUserAccessLevel();
	
    //Check user access
    if($user != $DIRECTOR && $user != $ADMIN && $user != $PRINCIPLE)
    {
        header('Location: login.php?page=manage-semesters&error=To access the manage semesters page, please log in as a director, principle or admin&logout=1');
        exit();
    }

    masterpage("Manage Semesters");

    
?>
    	
	<a href="add-new-semester.php">Add new semester</a>
	<br /><br />
    <table class="DataList">
	<thead>
		<tr>
			<th class="header" style="width:auto;">&nbsp;</th>
			<th class="header" style="width:auto;">&nbsp;</th>
			<th class="header">Semester Name</th>
			<th class="header">Program</th>
			<th class="header">Start</th>
			<th class="header">End</th>
		</tr>
	</thead>
	<tbody>
	<?php
		// This section retrieves the Semester data to fill in the table below.

		try
		{
			$link = connect_db();
			
			$query = "SELECT `semesterID`,`StartDate`,`EndDate`,`Description`,`Name` FROM `Semester`,`Program` where Semester.ProgramID = Program.ProgramID";

			$result = mysql_query($query, $link);
			$rowID = 0;

			if($result===false)
			{
				$_SESSION['error']="Could not get semester details.";
				return false;
			}
			else
			{
				while($semesterRow = mysql_fetch_array($result, MYSQL_ASSOC))
				{	
					$semesterID = $semesterRow["semesterID"];
					$startDate = $semesterRow["StartDate"];
					$endDate = $semesterRow["EndDate"];
					$desc = $semesterRow["Description"];
					$program = $semesterRow["Name"];


					if ($rowID == 0)
					{
						$rowClass = "rows";
						$rowID++;
					}
					else
					{
						$rowClass = "altrows";
						$rowID--;
					}
					echo "<tr><form action='edit-semesters.php' method='POST'><input type='hidden' name='semesterID' value='$semesterID'/>";
					$editButton = "<td class='$rowClass' style='width:auto;'>";
					$editButton .= "<input type='submit' value='Edit'></td>";
					echo $editButton;

					echo "</form>";

					echo "<form action='add-classes-to-semester.php' method='POST'><input type='hidden' name='semesterID' value='$semesterID'/>";
					$addClassesButton = "<td class='$rowClass' style='width:auto;'>";
					$addClassesButton .= "<input type='submit' value='Edit Classes'></td>";
					echo $addClassesButton;

					echo "</form>";
					echo "<td class='$rowClass' style='width:auto;'>$desc</td>";
					echo "<td class='$rowClass' style='width:auto;'>$program</td>";
					echo "<td class='$rowClass'>".DisplayDate($startDate)."</td>";
					echo "<td class='$rowClass'>".DisplayDate($endDate)."</td>";
					echo "</tr>";
				}
			}
		}
		catch (Exception $exception)
		{
			$_SESSION['error']=$exception->getMessage();
			return false;
		}
		
	?>
	</tbody>
    </table>

<?php
	endmasterpage();
?>
